import Cookies from 'js-cookie'
import Vue from 'vue'
import VueRouter from 'vue-router'
import Movie from '@/views/Movie/Movie.vue'
import movielist from '@/views/Movie/movielist.vue'
import moviedetial from '@/views/Movie/moviedetial.vue'
import changecity from '@/views/changecity/changecity.vue'
import Cinema from '@/views/Cinema/Cinema.vue'
import cinemaDetial from '@/views/Cinema/cinemaDetial.vue'
import store from '@/store'
import News from '@/views/News/News.vue'
import Mine from '@/views/Mine/Mine.vue'
import Minedetial from '@/views/Mine/Minedetial.vue'
import buyticket from '@/views/buyticket.vue'
import orderbyfilm from '@/views/orderbyfilm.vue'


Vue.use(VueRouter)



const routes = [{
    path: '/',
    redirect: '/Movies',
  },
  {
    path: '/Movies',
    name: 'Movies',
    component: Movie,
    redirect: '/Movies/ishot',
    children: [{ //电影列表
      path: '/Movies/:typess',
      name: 'movielist',
      component: movielist
    }]
  },
  {
    //影院
    path: '/Cinema',
    name: 'Cinema',
    component: Cinema
  },
  {//影院详情，不用子路由了
    path:'/Cinemas/:cinemaid',
    name:'cinemaDetial',
    component:cinemaDetial,
    meta: {
      footer: true //底栏是否消失，true为消失
    }
  },
  { //电影详情
    path: '/Movie/:id',
    component: moviedetial,
    name: 'moviedetial',
    meta: {
      footer: true //底栏是否消失，true为消失
    }
  },
  { //地址
    path: '/city',
    name: 'changecity',
    component: changecity,
    meta: {
      footer: true //底栏是否消失，true为消失
    }
  },
  { //咨询
    path: '/news',
    name: 'News',
    component: News,
    meta: {
      footer: true //底栏是否消失，true为消失
    }
  },
  { //我的
    path: '/mine',
    name: 'Mine',
    component: Mine
  },
  { //中心
    path: '/center',
    name: 'center',
    component: Minedetial
  },
  { //购票
    path: '/tickets/:id',
    name: 'buyticket',
    component: buyticket
  },
  {
    path: '/orderbyfilm/:id',
    name: 'orderbyfilm',
    component: orderbyfilm,
    meta: {
      footer: true //底栏是否消失，true为消失
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => { //没有城市选城市
  if (!Cookies.get('city') && to.path !== '/city') { //判断跳转对象是不是city，否则会死循环
    next({
      path: '/city'
    })
  } else {
    //添加cookie到store
    if(Cookies.get('city')){
      store.commit('changecity', JSON.parse(Cookies.get("city")))
    }
    next()
  }
})


export default router